package com.querycenter.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.querycenter.model.DataObject;
import com.querycenter.model.DataSource;
import com.querycenter.model.FieldDictionary;
import com.querycenter.serviceinter.DataObjectManageServiceInter;

import net.sf.json.JSONArray;

public class DataObjectManageAction {
	private DataObjectManageServiceInter doms = null;

	public DataObjectManageServiceInter getDoms() {
		return doms;
	}

	public void setDoms(DataObjectManageServiceInter doms) {
		this.doms = doms;
	}

	public void addDataObject() {
		System.out.println("addDataObject");
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			DataObject dobject = new DataObject();
			dobject.setDatasourceid(
					Integer.parseInt(request.getParameter("datasourceid")));
			dobject.setName(request.getParameter("name"));
			dobject.setDescription(request.getParameter("description"));
			dobject.setType(Integer.parseInt(request.getParameter("type")));
			dobject.setClassify(
					Integer.parseInt(request.getParameter("classify")));

			// doms.addDataObject(dobject);
			int id = doms.getDataObjectId(dobject);
			dobject.setId(id);// 查询新数据对象ID

			// 插入字段表数据，默认别名为空，权限为否
			List<FieldDictionary> fdList = doms.getField(dobject);

			for (int i = 0; i < fdList.size(); i++) {
				FieldDictionary fd = fdList.get(i);
				// 插入一条查询字段
				fd.setAuthoritytype(1);
				fd.setIsdefult(2);
				// doms.addField(fd);
				// 插入一条结果字段
				fd.setAuthoritytype(2);
				fd.setIsdefult(-1);
				// doms.addField(fd);

			}
			JSONArray jsonList = JSONArray.fromObject(fdList);
			// 返回页面展示字段，设置权限、别名
			ServletActionContext.getResponse()
					.setContentType("text/html;charset=UTF-8");
			ActionContext context = ActionContext.getContext();
			HttpServletResponse response = (HttpServletResponse) context
					.get(ServletActionContext.HTTP_RESPONSE);
			PrintWriter pw;
			pw = response.getWriter();
			pw.println(jsonList);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void updateFieldFlag() {
		System.out.println("updateFieldFlag");
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String str = request.getParameter("str");
			String[] trList = str.split("#");
			for (int i = 0; i < trList.length; i++) {
				String tr = trList[i];
				System.out.println(tr);
				String[] tdList = tr.split(",");
				// 创建一个字段对象
				FieldDictionary fd = new FieldDictionary();
				fd.setDataobjectid(Integer.parseInt(tdList[0]));
				fd.setField(tdList[1]);
				// 修改查询字段权限
				fd.setAuthoritytype(1);
				if (tdList[2].equals("true")) {
					fd.setAuthorityflag(1);
				} else {
					fd.setAuthorityflag(2);
				}
				if (tdList[4].equals("true")) {
					fd.setIsdefult(1);
				} else {
					fd.setIsdefult(2);
				}
				doms.updateFieldFlag(fd);
				// 修改结果字段权限
				fd.setAuthoritytype(2);
				if (tdList[3].equals("true")) {
					fd.setAuthorityflag(1);
				} else {
					fd.setAuthorityflag(2);
				}
				fd.setIsdefult(-1);
				doms.updateFieldFlag(fd);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addDataSource() {
		System.out.println("addDataSource");
		HttpServletRequest request = ServletActionContext.getRequest();
		DataSource ds = new DataSource();
		ds.setName(request.getParameter("name"));
		ds.setDatatype(Integer.parseInt(request.getParameter("datatype")));
		ds.setInstance(request.getParameter("instance"));
		ds.setIp(request.getParameter("ip"));
		ds.setPort(request.getParameter("port"));
		ds.setDatausername(request.getParameter("datausername"));
		ds.setDatapassword(request.getParameter("datapassword"));

		doms.addDataSource(ds);
	}
}